package com.groupfifteen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.groupfifteen.common.R;
import com.groupfifteen.entity.SysJudgeQuestionEntity;
import com.groupfifteen.entity.SysUserEntity;
import com.groupfifteen.service.SysJudgeQuestionService;
import com.groupfifteen.util.ApiResultHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("sysjudgequestion")
public class SysJudgeQuestionController {

    @Autowired
    private SysJudgeQuestionService sysJudgeQuestionService;

    @PostMapping("/query")
    public IPage<SysJudgeQuestionEntity> querySysJudgeQuestions(@RequestBody SysJudgeQuestionEntity sysJudgeQuestionEntity,
                                                                @RequestParam(defaultValue = "1") int pageNo,
                                                                @RequestParam(defaultValue = "10") int pageSize) {
        return sysJudgeQuestionService.querySysJudgeQuestions(sysJudgeQuestionEntity, pageNo, pageSize);
    }

    @PostMapping("addjudgequestion")
    public String AddJudgeQuestion(@RequestBody SysJudgeQuestionEntity sysJudgeQuestionEntity) {
        Integer result = sysJudgeQuestionService.AddJudgeQuestion(sysJudgeQuestionEntity);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    //
    @DeleteMapping("/deletejudgequestion/{question_id}")
    public String DeleteJudgeQuestion(@PathVariable("question_id") Integer question_id) {
        Integer result = sysJudgeQuestionService.DeleteJudgeQuestion(question_id);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }

    @GetMapping("/getjudgequestion/{question_id}")
    public SysJudgeQuestionEntity GetJudgeQuestion(@PathVariable("question_id") Integer question_id) {
        SysJudgeQuestionEntity result = sysJudgeQuestionService.GetJudgeQuestion(question_id);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }

    @PutMapping("/updatejudgequestion/{question_id}")
    public String UpdateJudgeQuestion(@PathVariable("question_id")Integer question_id,@RequestBody SysJudgeQuestionEntity sysJudgeQuestionEntity) {
        sysJudgeQuestionEntity.setQuestionId(question_id);
        Integer result = sysJudgeQuestionService.UpdateJudgeQuestion(sysJudgeQuestionEntity);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    /**
     * 查询最后一个判断题id
     *
     * @return 最后一个判断题
     */
    @GetMapping("/judgeQuestionId")
    public R<SysJudgeQuestionEntity> findOnlyQuestionId() {
        SysJudgeQuestionEntity res = sysJudgeQuestionService.findOnlyQuestionId();
        return  ApiResultHandler.buildApiResult(200,"查询成功",res);
    }

}

